package com.it.a10test4;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.UUID;

public class Server {
    public static void main(String[] args) throws IOException {
        //客户端：将本地文件上传到服务器。接收服务器的反馈。
        //服务器：接收客户端上传的文件，上传完毕之后给出反馈。

        //创建ServerSocket对象
        ServerSocket ss = new ServerSocket(8888);
        //等待接收
        Socket s = ss.accept();
        //接收客户端上传的文件，上传完毕之后给出反馈.
        BufferedInputStream bis = new BufferedInputStream(s.getInputStream());
        String name = UUID.randomUUID().toString().replace("-", "");
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\java\\basic-code\\mysockstnet\\severdir\\"+name+".jpeg"));
        byte[] buf = new byte[1024];
        int len;
        while ((len = bis.read(buf)) != -1) {
            bos.write(buf, 0, len);
        }
        //回写数据
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
        bw.write("上传成功");
        bw.newLine();
        bw.flush();
        //释放数据
        s.close();
        ss.close();

    }
}
